//
//  CXHGroMoreInterstitialAdapter.m
//  CXHAdSDK-GroMore
//
//  Created by 陈坤 on 2024/10/15.
//

#import "CXHGroMoreInterstitialAdapter.h"
#import <CXHAdSDK-Core/CXHAdSDKInterstitialAd.h>
#import <CXHAdSDK-Kit/CXHUtilsKit.h>


@interface CXHGroMoreInterstitialAdapter ()<CXHAdSDKInterstitialAdDelegate>

@property (nonatomic, strong) CXHAdSDKInterstitialAd *interstitialAd;

@end

@implementation CXHGroMoreInterstitialAdapter

- (BUMMediatedAdStatus)mediatedAdStatus {
    return BUMMediatedAdStatusNormal;
}

- (void)loadInterstitialAdWithSlotID:(NSString *)slotID andSize:(CGSize)size parameter:(NSDictionary *)parameter {
    if (self.interstitialAd) {
        self.interstitialAd.delegate = nil;
        self.interstitialAd = nil;
    }
    
    self.interstitialAd = [[CXHAdSDKInterstitialAd alloc] init];
    self.interstitialAd.delegate = self;
    self.interstitialAd.posId = slotID;
    [self.interstitialAd loadAdData];
}

- (BOOL)showAdFromRootViewController:(UIViewController *)viewController parameter:(NSDictionary *)parameter {
    self.interstitialAd.controller = viewController;
    [self.interstitialAd show];
    
    return YES;
}

- (void)didReceiveBidResult:(BUMMediaBidResult *)result {
    // 在此处理Client Bidding的结果回调
    if (result.win) {
        NSInteger winPrice = self.interstitialAd.getBidPrice;
        if (result.winnerPrice) {
            winPrice = result.winnerPrice;
        }
        [self.interstitialAd sendWinNotice:winPrice];
    } else {
        if (result.winnerPrice) {
            [self.interstitialAd sendLossNotice:result.winnerPrice];
        }
    }
}

#pragma mark - CXHAdSDKInterstitialAdDelegate
/**
 CXHAdSDKIntertitialAd请求成功回调
 
 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdSuccedToLoad:(CXHAdSDKInterstitialAd *)interstitialAd{
    [self.bridge interstitialAd:self didLoadWithExt:@{
        BUMMediaAdLoadingExtECPM : [NSString stringWithFormat:@"%ld", self.interstitialAd.getBidPrice],
    }];
}

/**
 CXHAdSDKIntertitialAd请求失败回调

 @param interstitialAd 插屏广告实例对象
 @param error 失败原因
*/
- (void)CXH_interstitialAdFailedToLoad:(CXHAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    
    [self.bridge interstitialAd:self didLoadFailWithError:error ext:@{}];
}

/**
 CXHAdSDKIntertitialAd展示在屏幕内回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidPresent:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

/**
 CXHAdSDKIntertitialAd展示在屏幕内失败回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdFailedToPresent:(CXHAdSDKInterstitialAd *)interstitialAd error:(NSError *)error{
    [self.bridge interstitialAdDidShowFailed:self error:error];
}

/**
 CXHAdSDKIntertitialAd点击回调

 @param interstitialAd 插屏广告实例对象
 @param loadingPageURL 广告落地页地址，当渠道为bwt，并且customLoadingPage为YES时有值
*/
- (void)CXH_interstitialAdDidClick:(CXHAdSDKInterstitialAd *)interstitialAd loadingPageURL:(NSString *)loadingPageURL{
    
    [self.bridge interstitialAdDidClick:self];
    [self.bridge interstitialAdWillPresentFullScreenModal:self];
}

/**
 CXHAdSDKIntertitialAd关闭回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidClose:(CXHAdSDKInterstitialAd *)interstitialAd{
    
    [self.bridge interstitialAdDidClose:self];
}

/**
 CXHAdSDKIntertitialAd展示回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdExposure:(CXHAdSDKInterstitialAd *)interstitialAd{
    [self.bridge interstitialAdDidVisible:self];
    
}

/**
 CXHAdSDKIntertitialAd展示上报回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdExposureReport:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}


/**
 CXHAdSDKIntertitialAd点击上报回调

 @param interstitialAd 插屏广告实例对象
*/
- (void)CXH_interstitialAdDidClickReport:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

/**
 CXHAdSDKIntertitialAd关闭落地页回调
 
 @param interstitialAd 广告实例
 */
- (void)CXH_interstitialAdCloseLandingPage:(CXHAdSDKInterstitialAd *)interstitialAd{
    
}

@end
